/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.taskfilters;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import listo.client.Formatter;
import listo.client.Lang;
import listo.client.model.Task;
import listo.client.model.Tasks;
import listo.utils.MiscUtils;
import listo.utils.types.DateTime;
import org.apache.commons.lang.StringUtils;

@Singleton
public class SearchFilter extends BaseFilter implements TaskFilter.Listener {

    private String searchText;
    private Tasks tasks;
    private final Lang lang;
    private final Provider<Formatter> formatterProvider;

    @Inject
    public SearchFilter(CompletionFilter completionFilter, Lang lang, Provider<Formatter> formatterProvider) {
        this.lang = lang;
        this.formatterProvider = formatterProvider;
        completionFilter.addFilterListener(this);
    }

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String searchText) {
        if (!MiscUtils.equals(searchText, this.searchText) && tasks != null) {
            this.searchText = searchText;
            update();
        } else {
            this.searchText = searchText;
        }
    }

    public void tasksChanged(Object updateObject) {
        tasks = (Tasks) updateObject;
        update();
    }

    private void update() {
        Tasks filteredTasks;
        if (StringUtils.isNotEmpty(searchText)) {
            filteredTasks = new Tasks();
            Formatter formatter = formatterProvider.get();
            for (Task task : tasks) {
                DateTime due = task.getDue();
                String dueString1 = due != null ? formatter.toNext9DaysString(due) : null;
                String dueString2 = due != null ? due.format(lang.getDateFormat()) : null;
                String timeString = due != null ? due.format(lang.getTimeFormat()) : null;
                if (StringUtils.containsIgnoreCase(task.getDesc(), searchText) ||
                        StringUtils.containsIgnoreCase(dueString1, searchText) ||
                        StringUtils.containsIgnoreCase(dueString2, searchText) ||
                        StringUtils.containsIgnoreCase(timeString, searchText) ||
                        StringUtils.containsIgnoreCase(formatter.
                                toLongReminderString(task.getDue(), task.getReminder()), searchText) ||
                        StringUtils.containsIgnoreCase(task.getNote(), searchText)) {
                    filteredTasks.add(task);
                }
            }
        } else {
            filteredTasks = tasks;
        }
        notifyListeners(filteredTasks);
    }

}